﻿using Lbb.Email.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Lbb.Email.Web.Areas.admin.Controllers
{
    public class EmailHelpController : Controller
    {
        //
        // GET: /admin/EmailHelp/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string firstName, string lastName, string email, string userID)
        {
            ViewBag.IsSuccess = false;
            try {
                GenerateEmail(firstName, lastName, email,userID);
                ViewBag.IsSuccess = true;
                ViewBag.ErrorMsg = "发送成功！";
            }
            catch(Exception e) {
                ViewBag.ErrorMsg = e.Message;
            }
            return View();
        }
        public void GenerateEmail(string firstName, string lastName, string email, string userID)
        {
            var password = Membership.GeneratePassword(Membership.MinRequiredPasswordLength, Membership.MinRequiredNonAlphanumericCharacters);
            var body ="<a href=www.abc.aspx?userID="+userID+">点此激活<a>以激活您的账号，您的密码：" + password+"请妥善保存以便更改！";
            System.Net.Mail.MailMessage MyMailer = new System.Net.Mail.MailMessage();
            MyMailer.Bcc.Add(email);
            MyMailer.Subject = "激活您的账号";
            MyMailer.Body = "亲爱的 " + lastName+firstName + ", <br/><br/>"+body;
            MyMailer.IsBodyHtml = Convert.ToBoolean(true);
            MyMailer.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            client.Send(MyMailer);
        }

    }
}
